package com.hq.signatureapplication;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    Button openButton;
    private Bitmap mSignBitmap;
    private String TAG = "MainActivity";

    WritePadDialog mWritePadDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openButton = findViewById(R.id.openButton);

        mWritePadDialog = new WritePadDialog(MainActivity.this,this, new WritePadDialog.WriteDialogListener() {
            @Override
            public void onPaintDone(Object object) {
                mSignBitmap = (Bitmap) object;
                if(mSignBitmap != null){
                    saveBitmap(mSignBitmap);
                    Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mWritePadDialog.setFullScreen(true);
        mWritePadDialog.setAccomplishClose(false);
        openButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWritePadDialog.show();
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        mWritePadDialog.onSizeChanged();

    }

    /** 保存方法 */
    public void saveBitmap(Bitmap bm) {
        Log.e(TAG, "保存图片");
        File f = new File("/sdcard/hqhsh/", "1.png");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Log.i(TAG, "已经保存");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}
